package com.bstek.mes.emp;

import com.bstek.bdf2.core.CoreHibernateDao;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.utils.AppUtils;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

@Repository(EmpDao.BEAN_ID)
public class EmpDao extends CoreHibernateDao {
	public static final String BEAN_ID="test.empDao";

	/**
	 * 分页查询所有的员工
	 * @author hans
	 * @param page
	 * @param parameter
	 */
	public void queryEmp(Page<Employee> page, Map<String, Object> parameter) {
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class);
		
		this.pagingQuery(page, dc);
	}
	/**
	 * 保存员工信息
	 * @author hans
	 * @param coll
	 */
	public void saveEmp(Collection<Employee> coll) {
		Session session=this.getSession();
		for(Employee emp:coll){
			EntityState state=EntityUtils.getState(emp);
			if(state.equals(EntityState.NEW)){
				emp.setId(AppUtils.generateUUID());
				emp.setCreateTime(new Date());
				emp.setCreateUser(ContextHolder.getLoginUserName());
				session.save(emp);
			}else if(state.equals(EntityState.MODIFIED)){
				session.update(emp);
			}else if(state.equals(EntityState.DELETED)){
				session.delete(emp);
			}else{
				
			}
		}
		
	}
	@Transactional
	public void save(Employee a){
		this.getSession().save(a);
	}
}
